import { fakeChartData, getFilterData, getGoodsPvData, getGoodsPayData, getFansChartData, getBaseAnalysis } from '../services/api';

export default {
  namespace: 'dataAnalysis',

  state: {

    loading: false,
    orderData : [],
    filterData : [],
    goodsPvRank : [],
    goodsPayRank : [],
    fansChartData : [],
    baseData : {}
  },

  effects: {
    *fetch({payload}, { call, put }) {
      const response = yield call(getBaseAnalysis, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            baseData : response
          },
        });
      }
    },
    *fetchSalesData(_, { call, put }) {
      const response = yield call(fakeChartData);
      if(response){
        yield put({
          type: 'save',
          payload: {
            salesData: response.salesData,
          },
        });
      }

    },
    *fetchFilter({ payload }, { call, put }) {
      const response = yield call(getFilterData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            filterData : response
          },
        });
      }
    },
    *fetchGoodsPv({ payload }, { call, put }) {
      const response = yield call(getGoodsPvData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            goodsPvRank : response
          },
        });
      }
    },
    *fetchGoodsPay({ payload }, { call, put }) {
      const response = yield call(getGoodsPayData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            goodsPayRank : response
          },
        });
      }
    },
    *fetchFansChart({ payload }, { call, put }) {
      const response = yield call(getFansChartData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            fansChartData : response
          },
        });
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {

        loading: false,
        orderData : [],
        filterData : [],
        goodsPvRank : [],
        goodsPayRank : [],
        fansChartData : [],
        baseData : {}

      };
    },
  },
};
